Home:ALL Converter>Join tables hibernate + group by

Join tables hibernate + group by

Ask Time:2012-06-22T04:20:29         Author:enfix

Json Formatter

I need to do this query with Java + Hibernate.

SELECT 
    table2.id,
    COUNT(table2.id) AS count
FROM
  table1 
    JOIN table2
       ON table1.fk_tb2 = table2.id  --many2one
GROUP BY
   table2.id

I would use DetachedCriteria class.....how can i do this ?

Author:enfix,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/11146105/join-tables-hibernate-group-by
Matt :

Try using projections like this:\n\nCriteria table1Crit = session.createCriteria(\"table1\");\nCriteria table2Crit = table1Crit.createCriteria(\"table2\", Criteria.INNER_JOIN);\ntable2Crit.setProjection( Property.forName(\"id\").count() );\n",
2012-06-21T23:30:24
yy